package base.net.socket.packet
{
	import flash.utils.ByteArray;
	
	import base.io.ByteBuffer;
	import base.types.SocketDataStructureType;

	/**
	 *数据包基类 
	 * @author Devin
	 */	
	public class SocketPacketBase extends ByteBuffer
	{
		/**
		 *数据包结构标识类型 
		 */		
		protected var _socketDataStructureType:int;
		/**
		 *数据包结构标识类型 
		 */
		public function get socketDataStructureType():int
		{
			return _socketDataStructureType;
		}
		
		/**
		 *包协议编号 
		 */		
		private var _packetCode:int;
		/**
		 *包协议编号 
		 */
		public function get packetCode():int
		{
			return _packetCode;
		}
		
		/**
		 *数据包基类 
		 * @param packetCode 包协议编号
		 * @param socketDataStructureType 数据包结构标识类型
		 * @param socketType 数据包类型
		 */		
		public function SocketPacketBase(packetCode:int, socketDataStructureType:int=SocketDataStructureType.DATA)
		{
			super();
			_packetCode=packetCode;
			_socketDataStructureType=socketDataStructureType;
		}
		
		/**
		 *读取UTF8字符 
		 * @return 返回取得成功的字符
		 */		
		override public function readUTF():String
		{
			var len:int=readInt();
			return super.readUTFBytes(len);
		}
		
		/**
		 *写入UTF8字符
		 */		
		override public function writeUTF(value:String):void
		{
			var bytes:ByteArray = new ByteArray();
			bytes.writeUTFBytes(value);
			writeInt(bytes.length);
			writeBytes(bytes);
		}
		
		/**
		 *资源释放 
		 */		
		public function dispose():void
		{
			clear();
		}
	}
}